package com.zzyl.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 线程池配置类
 */
@Configuration
public class ThreadPoolConfig {
	// 线程池维护线程的最少数量
	public static final int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors();
	// 线程池维护线程的最大数量
	public static final int MAX_POOL_SIZE = Runtime.getRuntime().availableProcessors() * 2;
	// 线程活动时间
	public static final int KEEP_ALIVE_TIME = 60;
	// 工作队列大小
	public static final int QUEUE_CAPACITY = 5000;
	
	@Bean
	public ExecutorService executorService() {
		AtomicInteger atomicInteger = new AtomicInteger(0);
		return new ThreadPoolExecutor(CORE_POOL_SIZE,
				MAX_POOL_SIZE,
				KEEP_ALIVE_TIME,
				TimeUnit.MILLISECONDS,
				new LinkedBlockingQueue<>(QUEUE_CAPACITY),
				r -> new Thread(r, "zzyl-pool" + atomicInteger.getAndIncrement()),
				new ThreadPoolExecutor.DiscardPolicy());
	}
	
	
}